﻿using NHibernate;
using NHibernate.Cfg;
using NHibernate.Linq;
using System;
using System.Collections.Generic;
using System.Linq;

namespace JuCloudy.Data.Nhibernate
{
    public class SysVariableContainer
    {
        private static Configuration mCfg = new Configuration();
        private static global::NHibernate.ISessionFactory SessionFactory = null;

        public SysVariableContainer(String TableName)
        {
            mCfg = new Configuration().Configure(
                System.Web.HttpContext.Current.Server.MapPath(
                     System.Configuration.ConfigurationManager.AppSettings["SysVariableNhCfg"]
                     ));
            
            SessionFactory = null;
            //mCfg.AddAssembly(Assembly.GetExecutingAssembly());
            mCfg.AddClass(typeof(JuCloudy.Data.Entity.SysVariable));
            SessionFactory = mCfg.BuildSessionFactory();
        }

        public List<JuCloudy.Data.Entity.SysVariable> Insert(JuCloudy.Data.Entity.SysVariable entity)
        {
            /*long rid = 0;
            ISession session = pool.GetResource(out rid);
            ITransaction vTransaction = session.BeginTransaction();
            try
            {
                session.SaveOrUpdate(entity);
                vTransaction.Commit();
                return session.Query<JuCloudy.Data.Entity.SysVariable>().ToList();
            }
            catch (Exception ex)
            {
                vTransaction.Rollback();
                throw ex;
            }
            finally
            {
                pool.ReturnResource(ref session, rid);
            }*/
            if (entity.CreateDateTimestamp == null || entity.CreateDateTimestamp <= 10000)
            {
                entity.CreateDate = DateTime.Now;
                entity.CreateDateTimestamp = JuCloudy.Data.timeStamp.ConvertDateTimeInt((DateTime)entity.CreateDate);
            }
            else
            {
                entity.LastUpdataDate = DateTime.Now;
                entity.LastUpdataDateTimestamp = JuCloudy.Data.timeStamp.ConvertDateTimeInt((DateTime)entity.LastUpdataDate);
            }

            using (ISession session = SessionFactory.OpenSession())
            {
                using (ITransaction vTransaction = session.BeginTransaction())
                {
                    try
                    {
                        session.SaveOrUpdate(entity);
                        vTransaction.Commit();
                        var query = session.Query<JuCloudy.Data.Entity.SysVariable>().ToList();
                        vTransaction.Dispose();
                        session.Flush();
                        session.Close();
                        session.Dispose();
                        return query;
                    }
                    catch (Exception ex)
                    {
                        vTransaction.Rollback();
                        vTransaction.Dispose(); 
                        session.Flush();
                        session.Close();
                        
                        session.Dispose();
                        throw ex;
                    }
                }

            }
        }

        public List<JuCloudy.Data.Entity.SysVariable> Delete(JuCloudy.Data.Entity.SysVariable entity)
        {
            /*long rid = 0;
            ISession session = pool.GetResource(out rid);
            ITransaction vTransaction = session.BeginTransaction();
            try
            {
                session.Delete(entity);
                vTransaction.Commit();
                return session.Query<JuCloudy.Data.Entity.SysVariable>().ToList();
            }
            catch (Exception ex)
            {
                vTransaction.Rollback();
                throw ex;
            }
            finally
            {
                pool.ReturnResource(ref session, rid);
            }*/

            using (ISession session = SessionFactory.OpenSession())
            {
                using (ITransaction vTransaction = session.BeginTransaction())
                {
                    try
                    {
                        session.Delete(entity);
                        vTransaction.Commit();
                        var query = session.Query<JuCloudy.Data.Entity.SysVariable>().ToList();
                        vTransaction.Dispose(); session.Flush();
                        session.Close();
                        session.Dispose();
                        return query;
                    }
                    catch (Exception ex)
                    {
                        vTransaction.Rollback();
                        vTransaction.Dispose();
                        session.Flush();
                        session.Close();
                        session.Dispose();
                        throw ex;
                    }
                }
            }
        }

        public JuCloudy.Data.Entity.SysVariable GetEntity(String Name)
        {
            using (ISession session = SessionFactory.OpenSession())
            {
                var query = session.Query<JuCloudy.Data.Entity.SysVariable>().First(p => p.Name == Name);

                session.Flush();
                session.Close();
                session.Dispose();
                return query;
            }
        }

        public JuCloudy.Data.Entity.SysVariable GetEntityById(int Id)
        {
            /*long rid=0;
            ISession session = pool.GetResource(out rid);
            var query = session.Query<JuCloudy.Data.Entity.SysVariable>().First(p => p.Id == Id);
            pool.ReturnResource(ref session, rid);
            return query;*/

            using (ISession session = SessionFactory.OpenSession())
            {
                var query = session.Query<JuCloudy.Data.Entity.SysVariable>().First(p => p.Id == Id);

                session.Flush();
                session.Close();
                session.Dispose();
                return query;
            }
        }

        public List<JuCloudy.Data.Entity.SysVariable> GetEntities()
        {
            /*long rid = 0;
            ISession session = pool.GetResource(out rid);
            var query = session.Query<JuCloudy.Data.Entity.SysVariable>().ToList();
            pool.ReturnResource(ref session,rid);
            return query;*/

            using (ISession session = SessionFactory.OpenSession())
            {
                var query = session.Query<JuCloudy.Data.Entity.SysVariable>().ToList();
                session.Flush();
                session.Close();
                session.Dispose();
                return query;
            }
        }
    }
}
